package com.bdqn.bookshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.bookshop.common.BookConstants;
import com.bdqn.bookshop.dao.GuideMapper;
import com.bdqn.bookshop.entity.Guide;
import com.bdqn.bookshop.service.GuideService;
import com.bdqn.bookshop.utils.DataGridViewResult;
import com.bdqn.bookshop.utils.FileResult;
import com.bdqn.bookshop.utils.FileUtil;
import com.bdqn.bookshop.utils.Result;
import com.bdqn.bookshop.vo.GuideVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 16:02
 **/
@Service
@Transactional
public class GuideServiceImpl extends ServiceImpl<GuideMapper, Guide> implements GuideService {

    @Override
    public Map<String, Object> uploadImage(MultipartFile file, HttpServletRequest request) {
        String newFileName = UUID.randomUUID().toString().replace("-", "").toUpperCase()+"_temp";
        Map<String,Object> map = new HashMap<String,Object>();
        FileResult fr = FileUtil.saveFileToPath(request,file, BookConstants.GUIDE_SAVE_PATH,BookConstants.GUIDE_MAPPING_URL,newFileName);
        if(fr!=null){
            List<String> list = (List<String>) request.getSession().getAttribute("guideImage");
            if(list==null){
                list = new ArrayList<String>();
            }
            list.add(fr.getFileName());
            request.getSession().setAttribute("guideImage",list);
            map.put("code", 0);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("src", fr.getHttpUrl());
            map.put("data", map1);

        }else{
            map.put("code", 1);
            map.put("msg", "图片上传失败");
        }
        return map;
    }

    @Override
    public Result addGuide(Guide guide, HttpServletRequest request) {
        List<String> list = (List<String>) request.getSession().getAttribute("guideImage");
        if(list!=null){
            for (String s : list) {
                if (guide.getContent().indexOf(s)>-1){
                    FileUtil.renameFile(BookConstants.GUIDE_SAVE_PATH,s);
                }
            }
        }
        guide.setChangeTime(new Date());
        guide.setCreateTime(new Date());
        guide.setContent(guide.getContent().replace("_temp",""));
        guide.setContent(guide.getContent().replace("p>","span>"));
        if(baseMapper.insert(guide)>0){
            return Result.createSuccess("添加成功");
        }
        return Result.createError("添加失败");
    }

    @Override
    public Result updateGuide(Guide guide, HttpServletRequest request) {
        List<String> list = (List<String>) request.getSession().getAttribute("guideImage");
        if(list!=null){
            for (String s : list) {
                if (guide.getContent().indexOf(s)>-1){
                    FileUtil.renameFile(BookConstants.GUIDE_SAVE_PATH,s);
                }
            }
        }
        guide.setChangeTime(new Date());
        guide.setContent(guide.getContent().replace("_temp",""));
        guide.setContent(guide.getContent());
        if(baseMapper.updateById(guide)>0){
            return Result.createSuccess("修改成功");
        }
        return Result.createError("修改失败");
    }

    @Override
    public Result deleteById(Integer id) {
        if(baseMapper.deleteById(id)>0){
            return  Result.createSuccess("删除成功");
        }
        return Result.createError("删除失败");
    }

    @Override
    public Result batchDelete(String ids) {
        String [] idArr = ids.split(",");
        if(baseMapper.deleteBatchIds(Arrays.asList(idArr))>0){
            return  Result.createSuccess("删除成功");
        }
        return Result.createError("删除失败");
    }

    @Override
    public DataGridViewResult guideList(GuideVo guideVo) {
        IPage<Guide> page = new Page<Guide>(guideVo.getPage(),guideVo.getLimit());
        QueryWrapper<Guide> queryWrapper = new QueryWrapper<Guide>();
        queryWrapper.like(StringUtils.isNotBlank(guideVo.getTitle()),"title",guideVo.getTitle());
        queryWrapper.ge(guideVo.getStartTime()!=null,"create_time",guideVo.getStartTime());
        queryWrapper.le(guideVo.getEndTime()!=null,"create_time",guideVo.getEndTime());
        baseMapper.selectPage(page,queryWrapper);
        return new DataGridViewResult(page.getTotal(),page.getRecords());
    }

    @Override
    public List<Guide> getGuideList() {
        return baseMapper.selectList();
    }

    @Override
    public List<Guide> getFourGuieList() {
        return baseMapper.selectFourList();
    }
}
